//
//  KXTitleButton.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/6.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

class KXTitleButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.setBackgroundImage(UIImage.imageWithStretchableName("navigationbar_filter_background_highlighted"), forState: UIControlState.Highlighted)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if self.currentImage == nil {
            return
        }
        // title
        self.titleLabel?.x = (self.imageView?.x)!
        self.imageView?.x = CGRectGetMaxX(self.titleLabel!.frame)
    }
    
    override func setTitle(title: String?, forState state: UIControlState) {
        super.setTitle(title, forState: state)
        self.sizeToFit()
    }
    
    override func setImage(image: UIImage?, forState state: UIControlState) {
        super.setImage(image, forState: state)
        self.sizeToFit()
    }
}
